#include "Product.h"

#include <iostream>
using namespace std;

/*
*	Constructors
*/
Product::Product(const char* productInfo) {
	int position = 0;

	filterWhiteSpace(position, productInfo);
	initializeProductNumber(position, productInfo);

	filterWhiteSpace(position, productInfo);
	initializeDescription(position, productInfo);

	filterWhiteSpace(position, productInfo);
	initializePrice(position, productInfo);

	filterWhiteSpace(position, productInfo);
	initializeIsTaxable(position, productInfo);

	filterWhiteSpace(position, productInfo);
	if (productInfo[position] != '\0' && productInfo[position] != '\n' && productInfo[position] != '\r') throw(INVALID_PRODUCT_INFO);
}

/*
*	Properties
*/
int Product::getProductNumber() { return productNumber; }
char* Product::getDescription() { return description;	}
float Product::getPrice()		{ return price;			}
bool Product::getIsTaxable()	{ return isTaxable;		}

/*
*	Methods
*/
void Product::filterWhiteSpace(int& position, const char* productInfo) {
	while (productInfo[position] == ' ' && productInfo[position] != '\t') ++position;
} //filterWhiteSpace

void Product::initializeProductNumber(int& position, const char* productInfo) {
	productNumber = 0;

	/*
	*	Build product number from numeric characters
	*/
	while (productInfo[position] != ' ') {
		if (productInfo[position] < '0' || productInfo[position] > '9') throw(INVALID_PRODUCT_INFO);

		productNumber = productNumber*10 + productInfo[position] - '0';
		++position;
	} //while

	if (productNumber > MAX_PRODUCT_NUMBER || productNumber < MIN_PRODUCT_NUMBER) throw(INVALID_PRODUCT_INFO);
} //initializeProductNumber

void Product::initializeDescription(int& position, const char* productInfo) {
	int i = 0;

	/*
	*	Read each character of product description to an array
	*
	*		Max of 12 characters		(imposed)
	*		No white spaces				(imposed)
	*/
	while (productInfo[position] != ' ') {
		if (productInfo[position] == '\0' || i == 12) throw(INVALID_PRODUCT_INFO);

		description[i++] = productInfo[position];
		++position;
	} //while

	description[i] = '\0';
} //initializeDescription

void Product::initializePrice(int& position, const char* productInfo) {
	/*
	*	Build price from numeric characters
	*/
	price = 0;
	while (productInfo[position] != '.' && productInfo[position] != ' ') {
		if (productInfo[position] < '0' || productInfo[position] > '9') throw(INVALID_PRODUCT_INFO);

		price = price*10 + productInfo[position] - '0';
		++position;
	} //while
	++position;

	/*
	*	Build decimals for product price from numeric characters (optional)
	*
	*		Max of 2 decimals
	*/
	int dec = 10;		//current amount to divide product decimal by
						//10, 100, 1000, etc for decimal places
	while (productInfo[position] != ' ') {
		if (productInfo[position] < '0' || productInfo[position] > '9' || dec == 1000) throw(INVALID_PRODUCT_INFO);

		price = price + (float)(productInfo[position] - '0')/dec;
		dec *= 10;

		++position;
	} //while
} //initializePrice

void Product::initializeIsTaxable(int& position, const char* productInfo) {
	/*
	*	Initialize is taxable
	*
	*		N or T, case sensitive
	*/

	switch (productInfo[position]) {
		case 'T':
			isTaxable = true;
			break;
		case 'N':
			isTaxable = false;
			break;
		default:
			throw(INVALID_PRODUCT_INFO);
	} //switch

	++position;
} //initializeIsTaxable